<!--
 * @Description: 促销方案
 * 
 * @Author: Edan<Edan>
 * @Date: 2024-07-12 09:10:19
 * @FilePath: /yuxiuproweb/src/views/app/activity/index.vue
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="活动编码" prop="activityId">
        <el-input
          v-model="queryParams.activityId"
          placeholder="请输入活动编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动名称" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-s-check"
          size="mini"
          @click="handleState(2)"
          >审核</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-position"
          size="mini"
          @click="handleState(3)"
          >发布</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="activityList"
      @selection-change="handleSelectionChange"
      :max-height="maxHeight"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <template v-for="(i, index) in tableForm.item">
        <el-table-column
          :key="index"
          :prop="i.itemcode"
          :columnKey="i.itemcode"
          :label="i.itemname"
          :align="i.aligntype ? i.aligntype : 'center'"
          :width="!!i.width ? i.width : ''"
          :min-width="i.minwidth"
          :show-overflow-tooltip="i.overflow ? true : false"
          :sortable="i.sortable ? true : false"
          :fixed="i.fixed"
          v-if="i.displaymark ? true : false"
        >
          <template slot-scope="scope">
            <div v-if="i.itemcode == 'status'">
              <span v-if="scope.row[i.itemcode] == 1">新建</span>
              <span v-if="scope.row[i.itemcode] == 2">审核</span>
              <span v-if="scope.row[i.itemcode] == 3">发布</span>
            </div>
            <div v-else-if="i.itemcode == 'payaccount'">
              <span v-if="scope.row[i.itemcode] == 1">现金</span>
              <span v-if="scope.row[i.itemcode] == 2">墙布</span>
              <span v-if="scope.row[i.itemcode] == 3">返利金</span>
            </div>
            <div v-else-if="i.itemcode == 'activityType'">
              <span v-if="scope.row[i.itemcode] == 1">普通促销</span>
              <span v-if="scope.row[i.itemcode] == 2">打包促销</span>
            </div>
            <div v-else-if="i.itemcode == 'content'">
              <div v-if="!scope.row[i.itemcode]">
                {{ scope.row[i.itemcode] }}
              </div>
              <div
                v-else
                v-html="scope.row[i.itemcode].replace(/\n/g, '<br>')"
              ></div>
            </div>
            <span v-else>{{ scope.row[i.itemcode] }}</span>
          </template>
        </el-table-column>
      </template>
      <el-table-column
        label="操作"
        align="center"
        width="280px"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleGoods(scope.row)"
            >促销商品</el-button
          >
          <el-button size="mini" type="text" @click="handleCustomer(scope.row)"
            >授权经销商</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            style="color: #ff9292"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <add-view
      v-if="open.show"
      :show.sync="open.show"
      v-bind="open"
      @update="getList"
    ></add-view>

    <GoodsView
      v-if="openGoods.show"
      :show.sync="openGoods.show"
      v-bind="openGoods"
      @update="getList"
    ></GoodsView>
    <CustomerView
      v-if="openCustomer.show"
      :show.sync="openCustomer.show"
      v-bind="openCustomer"
      @update="getList"
    ></CustomerView>
  </div>
</template>

<script>
import {
  getActivityList,
  delActivityList,
  updateActivityListItem,
  processActivityListItem,
  releaseActivityListItem,
} from "@/api/app/activity";
import AddView from "./mods/add.vue";
import GoodsView from "./mods/goods.vue";
import CustomerView from "./mods/customer.vue";
import { tableList } from "./mods/tablecolums.js";
import commonMix from "@/utils/mixins/common";
export default {
  name: "activity",
  components: {
    AddView,
    GoodsView,
    CustomerView,
  },
  mixins: [commonMix],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 墙布表格数据
      activityList: [],
      // 是否显示弹出层
      open: {
        show: false,
        type: "add",
        data: null,
      },
      openGoods: {
        show: false,
        data: null,
      },
      openCustomer: {
        show: false,
        data: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        activityId: null,
        activityName: null,
      },
      //
      tableForm: tableList,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询高端定制列表 */
    getList() {
      this.loading = true;
      var queryParams = {
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize,
        data: {
          activityId: this.queryParams.activityId,
          activityName: this.queryParams.activityName,
        },
      };
      getActivityList(queryParams).then((res) => {
        this.activityList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map((item) => item.activityId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.selRows = selection;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      const datarow = { ...row } || null;
      this.open = {
        show: true,
        type: "add",
        data: datarow,
      };
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = {
        show: true,
        type: "edit",
        data: {
          ...row,
        },
      };
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.activityId || this.ids;
      this.$modal
        .confirm("该操作不可逆，是否确定删除？")
        .then(function () {
          return delActivityList(projectIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //
    handleGoods(row) {
      this.openGoods = {
        show: true,
        data: {
          ...row,
        },
      };
    },
    handleCustomer(row) {
      this.openCustomer = {
        show: true,
        data: {
          ...row,
        },
      };
    },
    // 修改状态 审核or发布
    async handleState(type) {
      if (this.selRows.length != 1) {
        this.$message.warning("请选择一条活动");
        return;
      }

      // if (
      //   !this.selRows.every(
      //     (item) => item.activityType == this.selRows[0].activityType
      //   )
      // ) {
      //   this.$message.warning("请选择状态相同的活动");
      //   return;
      // }

      if (type == 2 && this.selRows[0].activityType == 3) {
        this.$message.warning("已发布的活动无需审核");
        return;
      }
      console.log(this.selRows[0]);
      if (type == 2) {
        // 审核
        processActivityListItem(this.selRows[0]).then((res) => {
          this.getList();
          this.$modal.msgSuccess("审核成功");
        });
      } else {
        // 发布
        releaseActivityListItem(this.selRows[0]).then((res) => {
          this.getList();
          this.$modal.msgSuccess("发部成功");
        });
      }
    },
  },
};
</script>
